package zhklappoint.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Optional;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import zhklappoint.dao.WXUserDao;
import zhklappoint.entity.WXUser;
import zhklappoint.enums.ENUM_ISDELETE;
import zhklappoint.enums.ENUM_SYSTEM;
import zhklappoint.service.WXUserService;
import zhklappoint.util.PageUtils;

@Service
public class WXUserServiceImpl implements WXUserService {

    @Autowired
    private WXUserDao wxuserDao;
    @Autowired
	private StringRedisTemplate stringRedisTemplate;
    
    @Override
    public Page<WXUser> findByCondition(WXUser wxuser, Integer page, Integer size) {
    	wxuser.setIsdelete(ENUM_ISDELETE.NOT_DELETED.value);
        Pageable pageable= PageUtils.toPageable(page, size, Sort.Direction.DESC, "createdate");
        ExampleMatcher matcher=ExampleMatcher.matching()
                .withMatcher("isdelete", ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("weixinOpenid", ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("username", ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("userId", ExampleMatcher.GenericPropertyMatchers.exact())
                .withMatcher("mobile", ExampleMatcher.GenericPropertyMatchers.exact())
                .withIgnorePaths("createdate","updatedate");// 忽略字段，即不管name是什么值都不加入查询条件
        Example<WXUser> example = Example.of(wxuser, matcher);
        return wxuserDao.findAll(example, pageable);
    }

    @Override
    public WXUser findByWeixinOpenid(String weixinOpenid) {
        return wxuserDao.findByWeixinOpenidAndIsdelete(weixinOpenid,ENUM_ISDELETE.NOT_DELETED.value);
    }

    @Override
    public List<WXUser> findByUsername(String userName) {
        return wxuserDao.findByUsernameAndIsdelete(userName,ENUM_ISDELETE.NOT_DELETED.value);
    }

    @Override
    public WXUser findById(String id) {
        Optional<WXUser> ou=wxuserDao.findById(id);
        if(!ou.isPresent()) {
            return null;
        }
        WXUser c=ou.get();
        if(c.getIsdelete()==ENUM_ISDELETE.DELETED.value){
            return null;
        }
        return c;
    }

    @Override
    public void save(WXUser wxUser) {
            wxUser.setUpdatedate(new Date().getTime());
            wxuserDao.save(wxUser);
    }
    @Transactional
	@Override
	public void updateFreeVoteLimit(String openId) {
		StringBuffer sql=new StringBuffer();
		sql.append("update zhkl_consumer_user zcu set zcu.times=zcu.times+1 where zcu.weixin_openid='"+openId+"'");
		//		System.out.println("tianjiayipaio:"+sql);
		wxuserDao.sqlQuery(sql.toString());
	}
    @Transactional
	@Override
	public void updateUserLimit() {
		StringBuffer sql=new StringBuffer();
		sql.append("update zhkl_consumer_user zcu set zcu.times=0 where into_way="+ENUM_SYSTEM.HTML5.value);
		//		System.out.println("ceshixianzhi::::"+sql);
		wxuserDao.sqlQuery(sql.toString());
	}

	

	@Override
	public List<WXUser> findByLastLoginIp(String ip) {
		
		return wxuserDao.findByRegisterIp(ip);
	}
}
